package com.example.smart_city_0829.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.entity.news.NewsList;
import com.example.smart_city_0829.mainFragment.MainFragment;
import com.example.smart_city_0829.retrofit.NetCallBack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Response;

public class SearchNewsResultActivity extends BaseActivity {
    private ListView lv;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_news_result;
    }

    @Override
    protected void initView() {
        lv = (ListView) findViewById(R.id.lv);

    }

    @Override
    protected void initData() {
        final String key = getIntent().getStringExtra("key");
        if (!"".equals(key)){
            final HashMap<String, Object> params = new HashMap<>();
            params.put("title",key);
            getNewsList(params);
        }
    }

    @Override
    protected void initEvent() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "搜索结果";
    }

    /**
     * 获取新闻列表
     */
    private void getNewsList(Map<String,Object> params){
        api.getNews(params).enqueue(new NetCallBack<NewsList>() {
            @Override
            public void onResponse(Call<NewsList> call, Response<NewsList> response) {
                final List<NewsList.RowsBean> data = response.body().getRows();
                lv.setAdapter(new MainFragment.NewsListAdapter(data,SearchNewsResultActivity.this));
            }
        });
    }
}